import { BodySetup } from "utils/creep/CreepSetup";
import { ActionBase, ActionBaseOfCreep } from "../ActionBase";
import { EActionCode } from "framework/FSM/Action/ActionCode";

/**
 * 升级Action
 */
export class ActionPickup  extends ActionBaseOfCreep<IActionCMD<"pickup">>
{
    protected updateArgs(action: IActionCMD<"pickup">): EActionCode 
    {
        if(super.updateArgs(action) !== EActionCode.OK)
        {
            return EActionCode.FAILED;
        }
        action.arg.resource = Game.getObjectById(action.arg.resource.id) as Resource;
        if(!action.arg.resource)
        {
            return EActionCode.FAILED;
        }
        
        if(action.arg.entity.store.getFreeCapacity() == 0)
        {
            return EActionCode.FAILED;
        }
        if(action.arg.entity.getNodeMemory().state && action.arg.entity.store.getUsedCapacity()>0)
        {
            return EActionCode.FAILED;
        }
        return EActionCode.OK;
    }
    protected _execute(action: IActionCMD<"pickup">): ActionCode 
    {
       
        const resource = action.arg.resource;
        const creep = action.arg.entity;
        if(resource.pos.distance(creep.pos)>1)
        {
            creep.mymoveTo({pos:resource.pos,range:1},{});
        }
        else
        {
            creep.pickup(resource);
            return   EActionCode.OK;
        }
        return   EActionCode.RUNNING;
    }
}